package com.dylhsh.fkglxt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dylhsh.fkglxt.service.*;
import com.dylhsh.fkglxt.model.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class UpController {
    @Reference
    private userService userService;
    @Reference
    private lfdjbService lfdjbService;
    @RequestMapping("upUser")
    public boolean upUser(User user){
        if (userService.upUser(user)!=0)
            return true;
        return false;
    }
    @RequestMapping("upLfdjb")
    public boolean upLfdjb(Lfdjb lfdjb){
        if (lfdjbService.upLfdjb(lfdjb)!=0)
            return true;
        return false;
    }

    @RequestMapping("auplfdjb")
    public boolean auplfdjb(@RequestParam Integer id,@RequestParam String field,@RequestParam String updateValue){
        Lfdjb lfdjb = lfdjbService.getLfdjbById(id);
        if (lfdjb==null)
            return false;
        if (field.equals("djy")){
            lfdjb.setDjy(updateValue);
        }
        if (field.equals("khdw")){
            lfdjb.setKhdw(updateValue);
        }
        if (field.equals("cph")){
            lfdjb.setCph(updateValue);
        }
        if (field.equals("kkhxm")){
            lfdjb.setKhxm(updateValue);
        }
        if (field.equals("rcsy")){
            lfdjb.setRcsy(updateValue);
        }
        if (field.equals("aqcs")){
            lfdjb.setAqcs(updateValue);
        }
        if (field.equals("zkwp")){
            lfdjb.setZkwp(updateValue);
        }
        if (field.equals("cwgbh")){
            lfdjb.setCwgbh(updateValue);
        }
        if (field.equals("sjh")){
            lfdjb.setSjh(updateValue);
        }
        if (lfdjbService.upLfdjb(lfdjb)!=0)
            return true;
        return false;
    }

    @RequestMapping("aupuser")
    public boolean aupnbuser(@RequestParam Integer id,@RequestParam String field,@RequestParam String updateValue){
        User user = userService.getUserById(id);
        if (user==null)
            return false;
        if (field.equals("realname")){
            user.setRealname(updateValue);
        }

        if (userService.upUser(user)!=0)
            return true;
        return false;
    }

    @RequestMapping("lfdjbrc")
    public boolean lfdjbrc(@RequestParam Integer id){
       Lfdjb lfdjb = lfdjbService.getLfdjbById(id);
       if (lfdjb==null)
           return false;
       else {
           if (lfdjb.getJcsj()!=null)
               return false;
           Date date = new Date();
           SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           lfdjb.setJcsj(sim.format(date));
           if (lfdjbService.upLfdjb(lfdjb)!=0)
               return true;
           return false;
       }
    }

    @RequestMapping("lfdjbcc")
    public boolean lfdjbcc(@RequestParam Integer id){
        Lfdjb lfdjb = lfdjbService.getLfdjbById(id);
        if (lfdjb==null)
            return false;
        else {
            if (lfdjb.getJcsj()==null)
                return false;
            Date date = new Date();
            long rightTime = (long) (date.getTime() + 8 * 60 * 60 * 1000); //把当前得到的时间用date.getTime()的方法写成时间戳的形式，再加上8小时对应的毫秒数
            date = new Date(rightTime);
            lfdjb.setCcsj(date);
            lfdjb.setSfjslc(1);
            if (lfdjbService.upLfdjb(lfdjb)!=0)
                return true;
            return false;
        }
    }
}
